package org.qb.mapreduce.partioner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        //1 获取配置信息以及获取JOB对象
        Configuration configuration = new Configuration();
        Job job = getJob(configuration);

        //6 设置输入和输出的路径
        FileInputFormat.setInputPaths(job,new Path("D:\\big data\\hadoop3.X\\资料\\11_input\\inputword"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\big data\\hadoop3.X\\output\\pationerTest1"));
        
        //7 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

    private static Job getJob(Configuration configuration) throws IOException {
        Job job = Job.getInstance(configuration);

        //2 关联本Driver程序的jar
        job.setJarByClass(WordCountDriver.class);

        //3 关联Mapper和Reducer的jar
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //4 设置Mapper输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //5 设置最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置partioner ==>只要设置ReduceTasks，就可以控制partioner，
        // 设置2个numReduceTask，就会输出两个succ 文件(0,1)，
        // numReuceTask > 1的情况才会走HashPartioner 方法
        //控制分区输出的值，如何控制呢？==》不重写Partioner的情况下是(默认是HashPartioner)
        // 无法控制输出到文件0还是文件1的，默认分区0的数据就输出到0，反之就是文件1
        job.setNumReduceTasks(2);

        //使用该参数插入重写的partioner类，让规定数据输出到规定文件里面
//        job.setPartitionerClass();



        return job;
    }
}
